package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class WorkspaceServiceImp implements WorkspaceService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper usersMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        //1.营业额
        Integer completed = Orders.COMPLETED;
        Double orderCount = getturnoverStatistics(begin, end, completed);
        //2.有效订单数
        Integer validOrderCount = getOrderCount(begin, end, completed);
        //3.订单完成率
        Integer orderCount1 = getOrderCount(begin, end, null);
        //订单完成率
        Double orderCompletionRate = 0.0;
        if(orderCount1 != 0 && orderCount1 != null){
            orderCompletionRate = (validOrderCount != null ? validOrderCount.doubleValue() : 0.0) / orderCount1;
        }
        //4.新增用户数
        Integer userCount = getUserCount(begin, end);
        //5.平均客单价 营业额/有效订单数
        Double unitPrice = 0.0;
        if(orderCount1 != 0 && orderCount1 != null){
            unitPrice = (orderCount != null ? orderCount : 0.0) / orderCount1;
        }
        return BusinessDataVO.builder()
                .turnover(orderCount != null ? orderCount : 0.0)
                .validOrderCount(validOrderCount != null ? validOrderCount : 0)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(userCount != null ? userCount : 0)
                .build();


    }
    /**
     * 各种订单状态的数量
     * */

    @Override
    public OrderOverViewVO getOrdersCount(LocalDateTime begin, LocalDateTime end) {
        //1.待接单
        Integer wait = Orders.TO_BE_CONFIRMED;
        Integer waitCount = getOrderCount(begin, end, wait);
        //2.派送中
        Integer delivery = Orders.CONFIRMED;
        Integer deliveryCount = getOrderCount(begin, end, delivery);
        //3.已完成
        Integer completed = Orders.COMPLETED;
        Integer completedCount = getOrderCount(begin, end, completed);
        //4.已取消
        Integer cancel = Orders.CANCELLED;
        Integer cancelCount = getOrderCount(begin, end, cancel);
        //5.全部订单数
        Integer totalCount = getOrderCount(begin, end, null);
        return OrderOverViewVO.builder()
                .waitingOrders(waitCount) //待接单
                .deliveredOrders(deliveryCount)//派送中
                .completedOrders(completedCount)//已完成
                .cancelledOrders(cancelCount)
                .allOrders(totalCount)
                .build();
    }
    /**
     * 菜品总览
     * */

    @Override
    public DishOverViewVO getDishOverView() {
        //TODO:1.已启售的菜品数量
        Integer dishCount =0;
        dishCount = getDishCount(1);
        //TODO:2.已停售的菜品数量
        Integer dishCount1 =0;
         dishCount1 = getDishCount(0);
        //
        return DishOverViewVO.builder()
                .sold(dishCount)
                .discontinued(dishCount1)
                .build();

    }
    /**
     * 套餐总览
     * */

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        //TODO:1.已启售的套餐数量
        Integer setmealCount = getSetmealCount(1);
        //TODO:2.已停售的套餐数量
        Integer setmealCount1 = getSetmealCount(0);
        //再添加数据之前判断是否为空，为空则返回0
        if (setmealCount == null) {
            setmealCount = 0;
            log.info("setmealCount为空");
        }
        if (setmealCount1 == null) {
            setmealCount1 = 0;
            log.info("setmealCount1为空");
        }
        return SetmealOverViewVO.builder()
                .sold(setmealCount)
                .discontinued(setmealCount1)
                .build();
    }


    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    public Double  getturnoverStatistics(LocalDateTime begin, LocalDateTime end, Integer status) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",status);
        Double turnover = ordersMapper.getturnoverStatistics(map);
        return turnover;
     }
    /**
     * 根据时间区间统计订单数量
     * @param beginTime
     * @param endTime
     * @return
     */
    private Integer getOrderCount(LocalDateTime beginTime, LocalDateTime endTime, Integer status) {
        Map map = new HashMap();
        map.put("begin",beginTime);
        map.put("end", endTime);
        map.put("status", status);
        return ordersMapper.getOrderCount(map);
    }
    /**
     * 根据时间区间统计用户数量
     * @param beginTime
     * @param endTime
     * @return
     */
    private Integer getUserCount(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = new HashMap();
        map.put("begin",beginTime);
        map.put("end", endTime);
        return usersMapper.getUserStatistics(map);
    }
    /***
     * 根据菜品状态统计菜品
     * @param status
     * @return
     */
    private Integer getDishCount(Integer status) {
        Map map = new HashMap();
        map.put("status",status);
        return dishMapper.getDishCount(map);
    }
    /**
     * 根据套餐状态统计套餐
     * @param status
     * @return
     */
    private Integer getSetmealCount(Integer status) {
        Map map = new HashMap();
        map.put("status",status);
        return setmealMapper.getSetmealCount(map);
    }

}
